package com.penghaisoft.microwms.material.web.outwarehouse.controller;

import com.penghaisoft.microwms.common.Constant;
import com.penghaisoft.microwms.common.controller.BaseController;
import com.penghaisoft.microwms.common.data.model.RawHandOut;
import com.penghaisoft.microwms.common.dto.Resp;
import com.penghaisoft.microwms.common.dto.TransResult;
import com.penghaisoft.microwms.common.page.Pager;
import com.penghaisoft.microwms.common.service.IWmsCommonService;
import com.penghaisoft.microwms.common.util.CommonUtils;
import com.penghaisoft.microwms.common.wms.model.ProHandIn;
import com.penghaisoft.microwms.material.web.outwarehouse.service.RawHandOutService;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * 原材料手工出库
 * Created by 王治国 on 2018/5/25.
 */
@RestController
@RequestMapping(value = "raw/rawHandOut")
@Slf4j
public class RawHandOutController extends BaseController {

    @Autowired
    private RawHandOutService rawHandOutService;
    
    @Autowired
	private IWmsCommonService wmsCommonService;

    @PostMapping("list")
    public TransResult findAll(@RequestParam(name = "page", defaultValue = "1") int page,
                               @RequestParam(name = "rows", defaultValue = "10") int rows, RawHandOut rawHandOut) {

        Pager<RawHandOut> result = rawHandOutService.findAll(page, rows, rawHandOut);
        return success(result);
    }

    @PostMapping("createOutOrder")
    public TransResult createOutOrder(RawHandOut condition){
        Date now = new Date();
        condition.setGmtCreate(now);
        condition.setGmtModified(now);
//        String operator = ShiroUtils.getLoginName();
//        condition.setCreateBy(operator);
//        condition.setLastModifiedBy(operator);

        Resp resp = new Resp();
//        resp = rawHandOutService.createSelfWarrant(condition);
        return fromResp(resp);
    }
    
	/** 
	* @Title: rawHandOutCreate 
	* @Description: 原材料手工出库任务批量创建
	* @param @param list
	* @param @return  参数说明 
	* @return TransResult    返回类型
	* @author jzh 
	* @throws 
	*/
	@PostMapping("/batchInsert")
	public TransResult rawHandOutCreate(@RequestBody List<RawHandOut> list) {
		TransResult transResult = new TransResult();
		if(list == null || list.isEmpty()){
			transResult.setCode(Constant.FAIL);
			transResult.setMsg("请选择成品型号及数量！");
			return transResult;
		}else{
			//生成手工出库单号
			String handOutNo = wmsCommonService.getProShortSeq5(Constant.SEQ_PREFIX_SHORT_SGC, Constant.SEQ_PREFIX_SHORT_REDIS_MAT_SGC, 1).get(0);
			//批量创建手工出库任务
			Resp resp = rawHandOutService.batchInsert(list,handOutNo, getLoginName());
			
			transResult.setCode(resp.getCode());
			transResult.setMsg(resp.getMsg());
		}
		
		return transResult;
	}

}
